/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.utils.ClassUtils;

import fever.data.crypt.ICrypter;
import fever.data.hasher.MD5;
import fever.Fever;
import fever.log.FeverDebug;
import fever.utils.Stringifier;

/**
 * Decrypts string content.
 * 
 * @see fever.data.libs.SecureConfigLoader
 * 
 * @author Romain Ecarnot
 */
class fever.data.libs.FileDecrypter
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _oCrypter : ICrypter;
	private var _sKey : String;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param crypter {@link fever.data.crypt.ICrypter} instance
	 * @param key Decryption key
	 * 
	 * @see setCrypter
	 * @see setKey
	 */ 
	public function FileDecrypter( crypter : ICrypter, key : String ) 
	{
		setCrypter( crypter );
		setKey( key );
	}
	
	/**
	 * Sets the crypter to use for file's decryption process.
	 * 
	 * @param crypter {@link fever.data.crypt.ICrypter} instance
	 */
	public function setCrypter( crypter : ICrypter ) : Void
	{
		if( !crypter )
		{
			FeverDebug.WARN( 'Invalid file decrypter : ' + crypter );
			
			_oCrypter = null;
		}
		else _oCrypter = crypter;
	}
	
	/**
	 * Returns used {@link fever.data.crypt.ICrypter}.
	 */
	public function getCrypter(  ) : ICrypter
	{	
		return _oCrypter;
	}
	
	/**
	 * Sets the crypter to use for file's decryption process.
	 * 
	 * @param key Decryption key. If not defined, use a default one using a 
	 * MD5 hash of current Fever application name
	 */
	public function setKey( key : String ) : Void
	{
		if( !key || !key.length > 0 )
		{
			FeverDebug.WARN( 'Invalid decryption key, use default' + key );
			
			_sKey = MD5.getProcess().hash( Fever.application.name );
		}
		else _sKey = key;
	}
	
	/**
	 * Returns decryption key.
	 */
	public function getKey(  ) : String
	{
		return _sKey;
	}
	
	/**
	 * Returns decrypted {@code src} string
	 */
	public function process( src : String ) : String
	{
		if( _oCrypter && _sKey ) return _oCrypter.decrypt( src, _sKey );
		else 
		{
			FeverDebug.ERROR( 'Invalid FileDecrypter : ' + this );
			return null;
		}
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this, ClassUtils.getClassName( _oCrypter ) + ' | ' + _sKey );
	}
}